package com.example.houqin_oa.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.comment_oa.pojos.PersonalEffects;
import com.example.comment_oa.pojos.PublicWare;
import com.example.comment_oa.pojos.ReplaceRecord;
import com.example.comment_oa.pojos_vo.LogisticsVo.PublicWareVo;
import com.example.houqin_oa.mapper.*;
import com.example.houqin_oa.service.ReplaceRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
* @author liang
* @description 针对表【replace_record】的数据库操作Service实现
* @createDate 2022-10-17 09:55:19
*/
@Service
public class ReplaceRecordServiceImpl extends ServiceImpl<ReplaceRecordMapper, ReplaceRecord>
    implements ReplaceRecordService{
    //维修
    @Autowired
    RepairRecordMapper repairRecordMapper;
    //更换
    @Autowired
    ReplaceRecordMapper replaceRecordMapper;
    //个人物品
    @Autowired
    PersonalEffectsMapper personalEffectsMapper;
    //公共物品
    @Autowired
    PublicWareMapper publicWareMapper;
    //仓库修改
    @Autowired
    EntrepotMapper entrepotMapper;

    @Override
    public List<ReplaceRecord> AllReplace (ReplaceRecord replaceRecord){
        replaceRecord.setProcessWhetherAdopt("1");
        replaceRecord.setReplaceState("0");
        return replaceRecordMapper.AllReplace(replaceRecord);
    }

    @Override
    public boolean Change(ReplaceRecord replaceRecord) {
        Date d = new Date();
        SimpleDateFormat sbf = new SimpleDateFormat("yyyyMMddHHmmss");

        replaceRecord.setCoverReplaceNo("GH"+sbf.format(d));
//
//        String str = replaceRecord.getReplaceNo();
//        if(str.substring(0,2).equals("LQ")){
//            PersonalEffects Per = personalEffectsMapper.selectNo(replaceRecord.getCoverReplaceNo());
//            PersonalEffects Eff = new PersonalEffects(null,replaceRecord.getCoverReplaceNo(),
//                    replaceRecord.getCoverReplaceName(),replaceRecord.getReplaceTime(),replaceRecord.getReplaceFrontAmount(),
//                    replaceRecord.getReplaceFrontSum(),Per.getPersonalState(),Per.getPersonalRecover(),Per.getUserId(),replaceRecord.getHisBelongings());
//            personalEffectsMapper.addRep(Eff);
//            System.err.println("111");
//            Eff.setPersonalState("2");
//            personalEffectsMapper.UpkeepDraw(Eff);
//            System.err.println("222");
//            entrepotMapper.upEntrepot(replaceRecord.getReplaceFrontAmount(),replaceRecord.getReplaceName());
//
//
//        }else if(str.substring(0,2).equals("GG")){
//            PublicWare pub = publicWareMapper.SelectNo(replaceRecord.getCoverReplaceNo());
//            PublicWare ware = new PublicWare(null,replaceRecord.getCoverReplaceNo(),replaceRecord.getCoverReplaceName(),
//                    replaceRecord.getReplaceTime(),pub.getPublicDept(),pub.getPublicBranch(),replaceRecord.getReplaceFrontSum(),replaceRecord.getReplaceFrontAmount()
//                    ,"0","",pub.getCoden());
//            publicWareMapper.addWare(ware);
//
//            pub.setPublicStatus("2");
//            publicWareMapper.upState(pub);
//        }
        replaceRecord.setReplaceState("1");
        return replaceRecordMapper.Change(replaceRecord);
    }
    //
    @Override
    public List<ReplaceRecord> RenewalApply(ReplaceRecord replaceRecord) {
        replaceRecord.setProcessWhetherAdopt("2");
        replaceRecord.setReplaceState("1");
        return replaceRecordMapper.RenewalApply(replaceRecord);
    }

    //更换物品
    @Override
    public boolean Renewal(ReplaceRecord replaceRecord) {
        replaceRecord.setReplaceTime(new Date());
        String str = replaceRecord.getReplaceNo();
        Date d = new Date();
        SimpleDateFormat sbf = new SimpleDateFormat("yyyyMMddHHmmss");
        //修改更换的判断
        boolean Tra = false ;
        boolean Add = false ;
        boolean New = false ;
        boolean UpEnt = false ;

        if(str.substring(0,2).equals("LQ")){
            replaceRecord.setCoverReplaceNo("LQ"+sbf.format(d));
            Tra = Transposing(replaceRecord);
            Add = AddPersonalItems(replaceRecord);
            UpEnt = upEntrepot(replaceRecord);

        }else if(str.substring(0,2).equals("GG")){
            replaceRecord.setCoverReplaceNo("GG"+sbf.format(d));
            Tra = Transposing(replaceRecord);
            New = NewPublicGoods(replaceRecord);
        }

        return true;
    }


    //修改仓库状态
    public boolean upEntrepot(ReplaceRecord replaceRecord){
        System.err.println(replaceRecord);
        return entrepotMapper.upEntrepot(replaceRecord.getReplaceFrontAmount(),replaceRecord.getReplaceName());
    }

    //更换状态修改
    public boolean Transposing(ReplaceRecord replaceRecord){
        replaceRecord.setReplaceState("1");
        return replaceRecordMapper.Transposing(replaceRecord);
    }
    //查询个人物品
    public PersonalEffects Consult(ReplaceRecord replaceRecord){
        return personalEffectsMapper.selectNo(replaceRecord.getReplaceNo());
    }
    //个人物品新增
    public boolean AddPersonalItems(ReplaceRecord r){
        PersonalEffects polling = Consult(r);
        PersonalEffects inquiry = new PersonalEffects(0,r.getCoverReplaceNo(),r.getCoverReplaceName(),r.getReplaceTime(),r.getReplaceFrontAmount(),r.getReplaceFrontSum(),"0",polling.getPersonalRecover(),polling.getUserId(),polling.getUserName());
        return personalEffectsMapper.AddPersonalItems(inquiry);
    }
    //查询公共物品
    public PublicWare About (ReplaceRecord replaceRecord){
        System.err.println(replaceRecord);
        return publicWareMapper.SelectNo(replaceRecord.getReplaceNo());
    }
    //公共物品新增
    public boolean NewPublicGoods(ReplaceRecord r){
        PublicWare port = About(r);
        PublicWareVo communal = new PublicWareVo(0,r.getCoverReplaceNo(),r.getCoverReplaceName(),r.getReplaceTime(),port.getPublicDept(),port.getPublicBranch(),r.getReplaceFrontSum(),r.getReplaceFrontAmount(),"0","",port.getCoden().getCodenNo());
        System.err.println(communal);
        return publicWareMapper.NewPublicGoods(communal);
    }


}




